import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:flutter_my_web/bean/response/get_text_response.dart';
import 'package:flutter_my_web/constants/Global.dart';
import 'package:flutter_my_web/pages/editarticle/SaveEditarticleDialogWidget.dart';
import 'package:flutter_my_web/pages/files/file_pages.dart';
import 'package:flutter_lib/utils/LogUtils.dart';
import 'package:flutter_my_web/utils/Utils.dart';
import 'package:flutter_my_web/widget/AppBarTitleWidget.dart';
import 'package:flutter_my_web/widget/BlueMaterialButton.dart';
import 'package:flutter_my_web/widget/TextWidget.dart';
import 'package:get/get.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:flutter_lib/utils/DpUtils.dart';

class EditarticlePage extends StatefulWidget {
  String? id;

  EditarticlePage({this.id});

  @override
  _WebEditartitcleState createState() => _WebEditartitcleState();
}

class _WebEditartitcleState extends State<EditarticlePage> {
  var dataInfo = "".obs;
  var editData = "".obs;
  var crossAxisCount = 2;
  var isReEdit = false;
  var textResponse = TextResponse();
  @override
  void initState() {
    super.initState();

    if (widget.id != null) {
      isReEdit = true;
    }
  }

  @override
  Widget build(BuildContext context) {
    // crossAxisCount = MediaQuery.of(context).size.width>1000?2:1;
    if (widget.id != null) {
      LogUtils.e("widget.link!:${widget.id!}");
      // FileManagerApi().getText(widget.id!).then((value) {
      //   textResponse = value;
      //   editData.value = value.content!;
      //   dataInfo.value = editData.value;
      // });
    }

    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
          elevation: 0,
          title: GestureDetector(
            onTap: () {},
            child: AppBarTitleWidget(
              isReEdit ? "编辑MD" : "创建MD",
            ),
          ),
          centerTitle: true,
          leading: IconButton(
              icon: Icon(
                Icons.arrow_back,
                color: Colors.white,
              ),
              onPressed: () {
                Get.back();
              })),
      body: Container(
        child: Row(
          children: [
            Expanded(
                child: Column(
              children: [
                Wrap(
                  children: [
                    BlueMaterialButton("MD编辑器1", () async {
                      var url = "https://tool.lu/markdown";
                      if (await canLaunch(url)) {
                        await launch(url);
                      } else {
                        throw 'url不能进行访问，异常。';
                      }
                    }),
                    BlueMaterialButton("MD编辑器2", () async {
                      var url = "https://markdownlivepreview.com/";
                      if (await canLaunch(url)) {
                        await launch(url);
                      } else {
                        throw 'url不能进行访问，异常。';
                      }
                    }),
                    BlueMaterialButton("文件夹", () {
                      if (!Global.isRoot) {
                        showSnackBar("管理员才能访问");
                        return;
                      }
                      Get.to(FilePage());
                    }),
                    BlueMaterialButton("预览", () {
                      dataInfo.value = editData.value;
                    }),
                    BlueMaterialButton("保存", () async {
                      if (dataInfo.value.isEmpty) {
                        Get.snackbar("错误", "内容不能为空");
                        return;
                      }
                      if (isReEdit) {
                        var bean = SaveEditarticleBean();
                        bean.coverStr = dataInfo.value;
                        bean.isEdit = true;
                        bean.id = textResponse.id!;
                        bean.titleStr = textResponse.title!;
                        bean.descStr = textResponse.desc!;
                        bean.coverStr = textResponse.coverUrl!;
                        bean.fileTagStr = textResponse.tags!;
                        bean.typeStr = textResponse.type!;
                        bean.saveContent = editData.value;
                        startSaveEditarticleDialogWidget(bean);
                      } else {
                        var bean = SaveEditarticleBean();
                        bean.saveContent = dataInfo.value;
                        startSaveEditarticleDialogWidget(bean);
                      }
                    }),
                  ],
                ),
                Center(child: SimpleText("外部编辑完再复制到输入框中预览最终效果")),
                Expanded(
                    child: Container(
                  padding: EdgeInsets.only(
                      left: 20.0.wDp,
                      right: 20.0.wDp,
                      top: 20.0.hDp,
                      bottom: 20.0.hDp),
                  child: Obx(() => TextField(
                        textAlign: TextAlign.start,
                        decoration: InputDecoration(
                            contentPadding: EdgeInsets.all(10.0),
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(15.0),
                            )),
                        controller: TextEditingController(text: editData.value),
                        onChanged: (change) {
                          editData.value = change;
                        },
                        expands: true,
                        maxLines: null,
                      )),
                )),
              ],
            )),
            Expanded(
                child: Column(
              children: [
                SimpleText("预览"),
                Expanded(
                    child: Obx(() => Markdown(
                          data: dataInfo.value,
                        )))
              ],
            ))
          ],
        ),
      ),
    );
  }
}
